//
//  HistoryMatchDetailHomeViewController.swift
//  FootballDataRecord
//
//  Created by Xx Jiang on 2020/1/10.
//  Copyright © 2020 Xx Jiang. All rights reserved.
//

import UIKit

class HistoryMatchDetailHomeViewController: BaseViewController,UIScrollViewDelegate {
        @IBOutlet weak var scrollView: UIScrollView!
        var pageControl:UIPageControl!
        var currenPage = 1
        var matchModel:MatchModel!
        override func viewWillAppear(_ animated: Bool) {
            let Bt = UIButton.init(type: .custom)
            Bt.frame = CGRect.init(x: 0, y: 0, width: 30, height: 44)
            Bt.setTitle("删除比赛", for: .normal)
            Bt.addTarget(self, action: #selector(clickdeleteBt), for: .touchUpInside)
            super.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: Bt)
        }
        @objc func clickdeleteBt()  {
           //删除比赛
            weak var __Weakself = self
            DHDataBaseOperate.deleteModelValueFromDB(model: matchModel, mainKey: DHMatchId) { (rs) in
                print("删除了id是\(__Weakself!.matchModel.matchId)的比赛,比赛是Name是\(__Weakself!.matchModel.matchName))")
            }
            self.navigationController?.popToRootViewController(animated: true)
        }
        override func viewDidLoad() {
            super.viewDidLoad()
            
            setupScrollView()
            
        }
        func setupScrollView() {
            self.scrollView.delegate = self
            self.scrollView.bounces = false
            self.scrollView.backgroundColor = .black
            self.scrollView.contentSize = CGSize.init(width: 3 * KScreenWidth, height: 0)
            self.view.addSubview(self.scrollView)
            
            let teamAVC = TeamDetailViewController()
            teamAVC.view.frame = CGRect.init(x: 0, y: 0, width:KScreenWidth, height: self.scrollView.frame.size.height)
            self.addChild(teamAVC)
            self.scrollView.addSubview(teamAVC.view)

            let gameDetail = HistoryMatchDetailViewController()
            gameDetail.view.frame = CGRect.init(x: KScreenWidth, y: 0, width:KScreenWidth, height: self.scrollView.frame.size.height)
            self.addChild(gameDetail)
            self.scrollView.addSubview(gameDetail.view)

            
            let teamBVC = TeamDetailViewController()
            self.addChild(teamBVC)
            teamBVC.view.frame = CGRect.init(x: KScreenWidth*2, y: 0, width:KScreenWidth, height: self.scrollView.frame.size.height)
            self.scrollView.addSubview(teamBVC.view)
            
            self.scrollView.isPagingEnabled = true
            self.pageControl = UIPageControl.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
            self.view.addSubview(pageControl)
            pageControl.numberOfPages = 3
            pageControl.currentPage = self.currenPage
            self.scrollView.contentOffset = CGPoint.init(x: KScreenWidth, y: 0)
            pageControl.currentPageIndicatorTintColor = UIColor.black
            pageControl.pageIndicatorTintColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
            pageControl.snp.makeConstraints { (make) in
                make.bottom.equalToSuperview().offset(-KBottomSafe)
                make.width.equalTo(100)
                make.height.equalTo(30)
                make.centerX.equalToSuperview()
            }
            pageControl.addTarget(self, action: #selector(clickPageControl), for: .touchUpInside)
            
        }
        @objc func clickPageControl(control:UIPageControl){
            print("currentpage\(pageControl.currentPage)")
            if currenPage != control.currentPage {
                 currenPage = pageControl.currentPage
                scrollView.contentOffset = CGPoint.init(x: Int(KScreenWidth) * control.currentPage , y: 0)
            }
        }
        
        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            let page = Int(scrollView.contentOffset.x / KScreenWidth)
            if  self.pageControl.currentPage != page{
                self.pageControl.currentPage = page
            }
            if page == 0{
                //发送通知告诉左边的控制器刷新数据
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeTeam`sPlayerData"), object: nil, userInfo: ["team":AddGameDetailViewController.TeamType.leftTeam,"matchModel":matchModel as Any])
            }
            if page == 2 {
                //发送通知告诉左边的控制器刷新数据
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeTeam`sPlayerData"), object: nil, userInfo: ["team":AddGameDetailViewController.TeamType.rightTeam,"matchModel":matchModel as Any])
            }
        }

        deinit {
            NotificationCenter.default.removeObserver(self)

            print("取消监听")
        }
}
